﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Idlesoft.Utils
{
	public static class DataExtensions
	{
		//provides for missing ItemArray() similar to the 'ItemArray' property available on the DataRow object
		public static object[] ItemArray(this DataColumn dc)
		{
			List<object> items = new List<object>();
			foreach (DataRow dr in dc.Table.Rows)
				items.Add(dr.ItemArray[dc.Ordinal]);
			return items.ToArray();
		}

		//column names
		public static string[] ColumnNames(this DataTable dt)
		{
			return dt.Columns.ColumnNames();
		}
		public static string[] ColumnNames(this DataColumnCollection dcc)
		{
			List<string> names = new List<string>();
			dcc.ForEachCol(dc => names.Add(dc.ColumnName));
			return names.ToArray();
		}

		//foreach: ds, dt, dtc, dr, dc
		public static void ForEach(this DataSet ds, Action<DataTable> action)
		{
			ds.Tables.ForEach(action);
		}
		public static void ForEach(this DataTableCollection dtc, Action<DataTable> action)
		{
			foreach (DataTable dt in dtc)
				action(dt);
		}
		public static void ForEach(this DataTable dt, Action<DataRow> action)
		{
			dt.Rows.ForEach(action);
		}
		public static void ForEach(this DataRowCollection drc, Action<DataRow> action)
		{
			foreach (DataRow dr in drc)
				action(dr);
		}
		public static void ForEachCol(this DataTable dt, Action<DataColumn> action)
		{
			dt.Columns.ForEachCol(action);
		}
		public static void ForEachCol(this DataColumnCollection dcc, Action<DataColumn> action)
		{
			foreach (DataColumn dc in dcc)
				action(dc);
		}
		public static void ForEach(this DataRow dr, Action<object> action)
		{
			dr.ItemArray.ForEach(action);
		}
		public static void ForEach(this DataColumn dc, Action<object> action)
		{
			dc.ItemArray().ForEach(action);
		}
		//head: ds, dt, dtc, dr, dc
		public static DataTable Head(this DataSet ds)
		{
			return ds.Tables.Cast<DataTable>().Head();
		}
		public static DataRow Head(this DataTable dt)
		{
			return dt.Rows.Cast<DataRow>().Head();
		}
		public static DataColumn HeadCol(this DataTable dt)
		{
			return dt.Columns.Cast<DataColumn>().Head();
		}
		public static object Head(this DataRow dr)
		{
			return dr.ItemArray.Head();
		}
		public static object Head(this DataColumn dc)
		{
			return dc.ItemArray().Head();
		}

		//tail: ds, dt, dtc, dr, dc
		public static IEnumerable<DataTable> Tail(this DataSet ds)
		{
			return ds.Tables.Cast<DataTable>().Tail();
		}
		public static IEnumerable<DataRow> Tail(this DataTable dt)
		{
			return dt.Rows.Cast<DataRow>().Tail();
		}
		public static IEnumerable<DataColumn> TailCol(this DataTable dt)
		{
			return dt.Columns.Cast<DataColumn>().Tail();
		}
		public static IEnumerable<object> Tail(this DataRow dr)
		{
			return dr.ItemArray.Tail();
		}
		public static IEnumerable<object> Tail(this DataColumn dc)
		{
			return dc.ItemArray().Tail();
		}

		//last: ds, dt, dtc, dr, dc
		public static DataTable Last(this DataSet ds)
		{
			return ds.Tables.Cast<DataTable>().Last();
		}
		public static DataRow Last(this DataTable dt)
		{
			return dt.Rows.Cast<DataRow>().Last();
		}
		public static DataColumn LastCol(this DataTable dt)
		{
			return dt.Columns.Cast<DataColumn>().Last();
		}
		public static object Last(this DataRow dr)
		{
			return dr.ItemArray.Last();
		}
		public static object Last(this DataColumn dc)
		{
			return dc.ItemArray().Last();
		}
	}
}
